<template>
  <div class="message" v-if="selectChatId != -1" @click="readMsg()">
    <header>
      <div class="friendname">{{selectChat.user.name}}</div>
    </header>
    <div class="message-wrapper" ref="list">
      <div class="fixed">
        <ul>
          <li class="message-item" v-for="(message, key) in selectChat.messages" :key="key">
            <div class="time" v-if="message.showDate">
              <span>{{message.date | time}}</span>
            </div>
            <div class="main" :class="{self: message.self}">
              <img :src="message.self? user.img: selectChat.user.img" alt class="avatar" />
              <div class="content">
                <div class="text" v-html="message.content">hello 小灰灰</div>
              </div>
            </div>
          </li>
        </ul>
      </div>
    </div>
    <v-text></v-text>
  </div>
</template>

<script>
import vText from "./v-text";
import { mapState, mapGetters, mapActions } from "vuex";

export default {
  components: {
    vText
  },
  computed: {
    ...mapState(["selectChatId", "user"]),
    ...mapGetters(["selectChat"])
    /* messages() {
      const chat = this.selectChat;
      if (chat) {
        return chat.messages;
      }
      let messages = [];
      return messages;
    } */
  },
  mounted() {
    //  在页面加载时让信息滚动到最下面
    setTimeout(() => {
      if (this.$refs.list) {
        this.$refs.list.scrollTop = this.$refs.list.scrollHeight;
      }
    }, 0);
  },
  methods: {
    ...mapActions(["readMsg"])
  },
  watch: {
    // 发送信息后,让信息滚动到最下面
    /* messages() {
      setTimeout(() => {
        if (this.$refs.list) {
          this.$refs.list.scrollTop = this.$refs.list.scrollHeight;
        }
      }, 0);
    } */
    selectChat: {
      handler() {
        setTimeout(() => {
          if (this.$refs.list) {
            this.$refs.list.scrollTop = this.$refs.list.scrollHeight;
          }
        }, 0);
      },
      deep: true
    }
  },
  filters: {
    // 将日期过滤为 hour:minutes
    time(date) {
      if (typeof date === "string") {
        date = new Date(date);
      }
      if (date.getMinutes() < 10) {
        return date.getHours() + ":0" + date.getMinutes();
      } else {
        return date.getHours() + ":" + date.getMinutes();
      }
    }
  }
};
</script>

<style scoped>
.message {
  width: 100%;
  height: 600px;
}

header {
  height: 60px;
  padding: 28px 0 0 30px;
  box-sizing: border-box;
  border-bottom: 1px solid #e7e7e7;
}

.friendname {
  font-size: 18px;
}

.message-wrapper {
  min-height: 390px;
  max-height: 390px;
  box-sizing: border-box;
  overflow-y: auto;
  border-bottom: 1px solid #e7e7e7;
}

.message-wrapper::-webkit-scrollbar {
  display: none;
}

.fixed {
  display: flex;
  justify-content: center;
  margin-left: calc(555px - 100%);
}

.fixed ul {
  width: 510px;
}

.time {
  width: 100%;
  font-size: 12px;
  margin: 20px auto 0 auto;
  text-align: center;
}

.time span {
  display: inline-block;
  padding: 4px 6px;
  color: #fff;
  border-radius: 3px;
  background-color: #dcdcdc;
}

.main {
  margin: 15px auto;
}

.main .avatar {
  width: 36px;
  height: 36px;
  float: left;
  margin: 0 10px;
  border-radius: 3px;
}

.main .content {
  display: inline-block;
  /* margin-left: 10px; */
  position: relative;
  padding: 6px 10px;
  max-width: 330px;
  min-height: 36px;
  line-height: 24px;
  box-sizing: border-box;
  font-size: 14px;
  text-align: left;
  word-break: break-all;
  background-color: #fafafa;
  border-radius: 4px;
}

.main .content:before {
  content: "";
  position: absolute;
  top: 12px;
  right: 100%;
  border: 6px solid transparent;
  border-right-color: #fafafa;
}

.self {
  text-align: right;
}

.self .avatar {
  float: right;
  /* margin: 0 15px; */
}

.self .content {
  background-color: #b2e281;
}

.self .content:before {
  right: -12px;
  vertical-align: middle;
  border-right-color: transparent;
  border-left-color: #b2e281;
}
</style>